home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10902 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  111 lines

  1. Path: news.uiowa.edu!ozone!maclenna
  2. From: maclenna@ozone.uiowa.edu (Mark MacLennan)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How can I include IOSTREAM.H only once?
  5. Date: 11 Mar 1996 07:33:49 GMT
  6. Organization: University of Iowa, Iowa City, IA, USA
  7. Distribution: world
  8. Message-ID: <4i0l0t$17h0@flood.weeg.uiowa.edu>
  9. References: <4hsgid$pmm@sam.inforamp.net> <4htg6a$593@news4.digex.net> <4i0fs4$5g9@sam.inforamp.net>
  10. Reply-To: maclenna@cgrer.uiowa.edu (Mark MacLennan)
  11. NNTP-Posting-Host: ozone.cgrer.uiowa.edu
  12.  
  13. In article <4i0fs4$5g9@sam.inforamp.net> rmorin@inforamp.net 
  14. (Randy Charles Morin) writes:
  15. >In article <4htg6a$593@news4.digex.net>, ell@access1.digex.net (Ell) wrote:
  16. >>Randy Charles Morin (rmorin@inforamp.net) wrote:
  17. >>: >#ifndef __STDIO__
  18. >>: >#include <stdio.h>
  19. >>: >#endif
  20. >>
  21. >>And generally the '#endif' is the last of code in the file.
  22. >>#ifndef __STDIO__
  23. >>#include <stdio.h>
  24. >> /*...code...*/
  25. >> /*...code...*/
  26. >>#endif
  27. >
  28. >Sorry, but this is wrong.  Everybody, including myself is almost always 
  29. >wrong, so let's not belabor the point.  What I was attempting to do is include 
  30. >a file only if it has not already been included.  Surely the file <stdio.h> 
  31. >has simliar pre-compile statements to what you describe, but the include file 
  32. >will still be loaded during pre-compilation without using the pre-compile 
  33. >statements I describe.  My statement load the file only if they have not 
  34. >been pre-loaded, as oppose to the other statements compiling the file only if 
  35. >they have not been previously compiled.  
  36.  
  37. Actually the above example is essentially right and your examples are
  38. bizzare.  To make sure an include file is only included once, no matter
  39. what, the include file would contain the following pre-processor 
  40. statements:
  41.  
  42. #ifndef _MY_H_FILE_
  43. #define _MY_H_FILE_ 
  44. /*...code...*/
  45. /*...code...*/
  46. #endif 
  47.  
  48. Since the pre-processor will only have to examine the first #ifndef
  49. statement, very little time is incurred when loading such files -
  50. so trivial as to be irrelevant to the total compilation time.
  51. Indeed, the above idiom is very widely used for C++ programs and
  52. all relevant system include files are usually implemented in this way.
  53. (although sometimes "#pragma once" is used instead of the above
  54. but this is not very portable.)
  55.  
  56. Hey, guess what!  You learned something!
  57.  
  58. - MARK
  59.  
  60.  
  61. >
  62. >Example:
  63. >
  64. >header.h
  65. >--------
  66. >#ifndef HEADER_H
  67. >#define HEADER_H
  68. >typedef bool int;
  69. >#endif
  70. >------
  71. >
  72. >source.cpp
  73. >----------
  74. >#ifndef HEADER_H
  75. >#include "header.h"
  76. >#endif
  77. >#ifndef HEADER_H
  78. >#include "header.h"
  79. >#endif
  80. >int main()
  81. >{
  82. >    bool b;
  83. >    int i=0;
  84. >    b=i;
  85. >    return b;
  86. >};
  87. >-----
  88. >
  89. >source2.cpp
  90. >----------
  91. >#include "header.h"
  92. >#include "header.h"
  93. >int main()
  94. >{
  95. >    bool b;
  96. >    int i=0;
  97. >    b=i;
  98. >    return b;
  99. >};
  100. >-----
  101. >
  102. >source.cpp will compile faster then source2.cpp because the first source loads 
  103. >the header file once, while the second source loads the header file twice.  
  104. >This is a simply example and no noticeable compile time savings will occur.  
  105. >But if all standard headers used this type of syntax, then compile times would 
  106. >greatly increase.
  107. >
  108. >Agrivar
  109.  
  110.  
  111.